import os


'''
判断输入的用户账号是否在原有文件中存在
'''
def select_user_num(user_num):
    if not os.path.exists(f'./user.txt'):
        with open('./user.txt', 'w', encoding='utf-8'):
            pass
    with open('./user.txt', 'r', encoding='utf-8') as f:
        for line in f:
            num=line.split(':')[1]
            if num == user_num:
                return False
        return True

'''
将成功注册的账号信息存入文件
'''
def register_user(user_name,user_num,user_password):
    with open('./user.txt', 'a', encoding='utf-8') as f:
        f.write(f'{user_name}:{user_num}:{user_password}\n')
'''
判断输入的账号与密码是否匹配
'''
def select_user_num_and_password(user_num,user_password):
    if not os.path.exists(f'./user.txt'):
        return False
    else:
        with open('./user.txt', 'r', encoding='utf-8') as f:
            for line in f:
                num=line.split(':')[1]
                if num == user_num:
                    password=line.split(':')[2].strip()
                    if password == user_password:
                        return True
                    else:
                        return False
            return False


"""获取用户名"""
def get_username(user_num):
    if not os.path.exists(f'./user.txt'):
        return None
    else:
        with open('./user.txt', 'r', encoding='utf-8') as f:
            for line in f:
                num=line.split(':')[1]
                if num == user_num:
                    return line.split(':')[0]
    return None

'''
 服务端将接收到的文件写入
'''
def in_save(path,file_size,file_name,conn):
    if not os.path.exists(path):
        os.makedirs(path)
    with open(os.path.join(path, file_name), 'ab') as f:
        while file_size>0:
            if file_size<4096:
                temp=conn.recv(file_size)
            else:
                temp=conn.recv(4096)
            f.write(temp)
            file_size-=len(temp)
    print('文件写入成功')


'''
客户端读取文件并发送
'''
def out_save(path,conn,tqdm):
    with open(path, 'rb') as f:
        while True:
            data=f.read(4096)
            if not data:
                break
            conn.send(data)
            tqdm.update(len(data))

        tqdm.close()
        print('文件发送成功')

'''
获取user_num的通讯录名单
'''
def get_contacts(user_num,user_name):
    list_=[]
    if not os.path.exists(f'./db/用户信息/(账号{user_num}名字{user_name})'):
        os.makedirs(f'./db/用户信息/(账号{user_num}名字{user_name})')
    if not os.path.exists(f'./db/用户信息/(账号{user_num}名字{user_name})/通讯录.txt'):
        with open(f'./db/用户信息/(账号{user_num}名字{user_name})/通讯录.txt', 'w', encoding='utf-8') as f:
            pass
    with open(f'./db/用户信息/(账号{user_num}名字{user_name})/通讯录.txt', 'r', encoding='utf-8') as f:
        for line in f:
            name=line.split(':')[0]
            num=line.split(':')[1]
            data={
                'name':name,
                'user_num':num,
            }
            list_.append(data)
    return list_

'''
向user_num的通讯录中添加联系人
'''
def in_contacts(user_num,contact_num):
    user_name=get_username(user_num)
    contact_name=get_username(contact_num)
    if not os.path.exists(f'./db/用户信息/(账号{user_num}名字{user_name})'):
        os.makedirs(f'./db/用户信息/(账号{user_num}名字{user_name})')
    with open(f'./db/用户信息/(账号{user_num}名字{user_name})/通讯录.txt', 'a', encoding='utf-8') as f:
        f.write(f'{contact_name}:{contact_num}:\n')
        print('添加成功')

'''
将用户私信存储起来,其中第一个写入为发信人，第二个为收信人，第三个为内容，第四个为时间
'''
def message_in_file(user1_num,user2_num,content,time):
    with open(f'./db./私信消息记录.txt', 'a', encoding='utf-8') as f:
        f.write(f'{user1_num}:{user2_num}:{content}:{time}:\n')


'''
取出对应user_num的所有消息，并将其列表返回
'''
def message_out_file(user_num1,user_num2):
    lis=[]
    if not os.path.exists(f'./db./私信消息记录.txt'):
        with open(f'./db./私信消息记录.txt','w', encoding='utf-8') as f:
            pass
    with open(f'./db./私信消息记录.txt','r', encoding='utf-8') as f:
        for line in f:
            if (user_num1==line.split(':')[0] and user_num2==line.split(':')[1]) or (user_num1==line.split(':')[1] and user_num2==line.split(':')[0]):
                dic={'发信人':line.split(':')[0],'收信人':line.split(':')[1],'content':line.split(':')[2],'time':line.split(':')[3]}
                lis.append(dic)
    return lis




